package hu.diploma.web.server.serviceimpl;

import hu.diploma.common.enums.DataSubType;
import hu.diploma.common.enums.DataType;
import hu.diploma.common.service.PersonService;
import hu.diploma.common.vos.AddressVo;
import hu.diploma.common.vos.PersonVo;
import hu.diploma.common.vos.PersonalDataVo;
import hu.diploma.common.vos.StructuredNameVo;
import hu.diploma.core.util.ServiceProviderImpl;
import hu.diploma.web.client.service.WebContactService;
import hu.diploma.web.shared.ComboBoxItem;
import hu.diploma.web.shared.webvos.AddressWebVo;
import hu.diploma.web.shared.webvos.ContactWebVo;
import hu.diploma.web.shared.webvos.PersonalDataWebvo;
import hu.diploma.web.shared.webvos.StructuredNameWebVo;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
public class WebContactServiceImpl extends RemoteServiceServlet implements WebContactService {
	public static DozerBeanMapper beanMapper = new DozerBeanMapper();
	private List<ContactWebVo> list;
	private final PersonService personService = ServiceProviderImpl.getServiceProvider().getPersonService();

	public List<ContactWebVo> loadAll() throws Exception {

		try {

			List<PersonVo> res = personService.loadAll();
			list = new ArrayList<ContactWebVo>();
			for (PersonVo personVo : res) {

				StructuredNameVo structuredName = personVo.getStructuredName();
				ContactWebVo personWebVo = new ContactWebVo();
				personWebVo.setId(personVo.getId());
				StructuredNameWebVo nameWebVo = new StructuredNameWebVo();
				nameWebVo.setId(structuredName.getId());
				nameWebVo.setDisplayName(structuredName.getDisplayName());
				nameWebVo.setFamilName(structuredName.getFamilName());
				nameWebVo.setGivenName(structuredName.getGivenName());
				nameWebVo.setMiddleName(structuredName.getMiddleName());
				nameWebVo.setPreixName(structuredName.getPreixName());
				personWebVo.setDisplayName(nameWebVo.getDisplayName());

				List<AddressWebVo> addressWebVos = new ArrayList<AddressWebVo>();
				List<AddressVo> addressVos = personVo.getAddresses();
				for (AddressVo addressVo : addressVos) {
					AddressWebVo addressWebVo = beanMapper.map(addressVo, AddressWebVo.class);
					addressWebVos.add(addressWebVo);
				}
				addressWebVos.add(new AddressWebVo());
				personWebVo.setAddressWebVos(addressWebVos);
				personWebVo.setNameWebVo(nameWebVo);
				if (personVo.getPersonalDatas() != null) {
					List<PersonalDataWebvo> personalDatas = new ArrayList<PersonalDataWebvo>();
					for (PersonalDataVo dataVo : personVo.getPersonalDatas()) {

						PersonalDataWebvo dataWebvo = new PersonalDataWebvo();
						dataWebvo.setId(dataVo.getId());
						dataWebvo.setSubType(new ComboBoxItem(dataVo.getSubtype().toString(), ""));
						dataWebvo.setSubTypeText(dataVo.getSubtype().toString());
						dataWebvo.setValue(dataVo.getValue());
						dataWebvo.setType(dataVo.getType().toString());
						personalDatas.add(dataWebvo);

						if (dataVo.getType().equals(DataType.EMAIL)) {
							personWebVo.setEmail(dataVo.getValue());
						}

						if (dataVo.getType().equals(DataType.PHONE)) {
							personWebVo.setPhone(dataVo.getValue());
						}
					}
					personWebVo.setPersonalDatas(personalDatas);
				}

				// String groups = "";
				// for (GroupVo groupVo : personVo.getGroups()) {
				// groups += groupVo.getName();
				// }
				personWebVo.setGroups("Nincs besorolva");
				list.add(personWebVo);

			}
		} catch (Exception e) {
			GWT.log("loadAll", e);
		}
		return list;
	}

	public ContactWebVo loadPerson(Long id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public void deletePerson(ContactWebVo personWebVo) throws Exception {
		PersonVo vo = new PersonVo();
		vo.setId(personWebVo.getId());

		personService.delete(vo);
	}

	public ContactWebVo updatePerson(ContactWebVo personWebVo) throws Exception {

		PersonVo vo = personService.findById(personWebVo.getId());
		if (vo == null) {
			vo = new PersonVo();
		}

		StructuredNameVo nameVo = beanMapper.map(personWebVo.getNameWebVo(), StructuredNameVo.class);

		List<PersonalDataVo> personalDatas = new ArrayList<PersonalDataVo>();
		vo.setStructuredName(nameVo);

		if ((personWebVo.getPersonalDatas() != null) && (personWebVo.getPersonalDatas().size() > 0)) {
			for (PersonalDataWebvo dataWebvo : personWebVo.getPersonalDatas()) {
				PersonalDataVo dataVo = new PersonalDataVo();

				dataVo.setId(dataWebvo.getId());
				dataVo.setSubtype(DataSubType.valueOf(dataWebvo.getSubType().getValue()));
				dataVo.setType(DataType.valueOf(dataWebvo.getType()));
				dataVo.setValue(dataWebvo.getValue());
				personalDatas.add(dataVo);
			}
		}
		vo.setPersonalDatas(personalDatas);
		if (personWebVo.getId() != null) {
			personService.merge(vo);
		} else {
			personService.save(vo);
		}

		return personWebVo;
	}

	public List<ContactWebVo> loadByDevice(Long deviceid) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
